package com.oreilly.ez.base;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.time.LocalTime;

@Slf4j
@Configuration
public class ScheduleConfig {


    @Autowired
    private WebSocketService webSocketService;

//    @Bean
//    public TaskScheduler taskScheduler(){
//        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
//        taskScheduler.setPoolSize(8);
//        return taskScheduler;
//    }


    /**
     * 固定间隔任务(A,60s,A,60s,A....)
     */
    @Scheduled(fixedDelay = 1000*60)
    public void job1() {
        System.out.println(Thread.currentThread() + ", job1@" + LocalTime.now());
    }

    /**
     * cron 表达式
     * 每2分钟执行一次
     * 当方法的执行时间超过任务调度频率时，
     * 调度器会在下个周期执行。
     */
    @Scheduled(cron = "0 0/1 * * * *")
    public void job2() {
        System.out.println(Thread.currentThread() + ", job2@" + LocalTime.now());


        WiselyResponse response = new WiselyResponse();
        response.setMsg("timestamp=" + System.currentTimeMillis() + "### from server");
        webSocketService.sendMsg(response);
    }
}
